import pinia from '@/stores'
import { centerStore } from '@/stores/center'
import axios, { type AxiosRequestHeaders } from 'axios'
// 引入 NProgress
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 新建一个axios实例
const serve = axios.create({
  baseURL: '/api',
  timeout: 10000
})

// 添加请求拦截器
serve.interceptors.request.use(
  (config) => {
    // 要么携带请求头
    let token = localStorage.getItem("token_key")
		// 判断是否拿到token
		if(token){
			// 2.把拿到的token携带到请求头当中去
			(config.headers as AxiosRequestHeaders).token = token
		}

    // 开启进度条
    NProgress.start()
    // let token =localStorage.getItem('token_key')
    // // console.log(token);
    
    // if (token) {
    //   config.headers.token=token
    // }
    
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 添加响应拦截器
serve.interceptors.response.use(
  (response) => {
    // 隐藏进度条
    NProgress.done()
    return response.data
  },
  (error) => {
    // 隐藏进度条
    NProgress.done()
    return Promise.reject(error)
  }
)
export default serve
